package com.sebiotec.driver.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProviders;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.allen.library.RxHttpUtils;
import com.sebiotec.basecommon.utils.base.BaseActivity;
import com.sebiotec.basecommon.utils.tools.ActivityTool;

import com.sebiotec.driver.R;
import com.sebiotec.driver.R2;
import com.sebiotec.driver.model.post.PostPwd;
import com.sebiotec.driver.viewmodel.BasicViewModel;
import com.sebiotec.driver.viewmodel.UpdatePwdViewModel;
import com.xuexiang.xui.widget.toast.XToast;
import com.xw.repo.XEditText;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = "/driver/UpdatePwdActivity")
public class UpdatePwdActivity extends BaseActivity {


    @BindView(R2.id.qk_back)
    ImageView qkBack;
    @BindView(R2.id.tv_save)
    TextView tvSave;
    @BindView(R2.id.et_old)
    XEditText etOld;
    @BindView(R2.id.et_new)
    XEditText etNew;
    @BindView(R2.id.et_renew)
    XEditText etRenew;
    private UpdatePwdViewModel mUpdatePwdViewModel;
    private String token;

    @Override
    protected int setLayoutId() {
        return R.layout.driver_activity_update_pwd;
    }



    @Override
    protected void initUI() {
        if (getIntent().getBundleExtra("login") != null) {
            Bundle login = getIntent().getBundleExtra("login");
            token = login.getString("Token");

        }

        mUpdatePwdViewModel = ViewModelProviders.of(this).get(UpdatePwdViewModel.class);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setListener() {

    }

    @Override
    public void onBackPressed() {
        if (!TextUtils.isEmpty(token)) {
            //
            XToast.error(UpdatePwdActivity.this, R.string.plz_up_pwd).show();
        } else {
            super.onBackPressed();
        }

    }

// else if (!CheckPwd.checkPasswordRule(edPwd.getTextEx().toString().trim())){
//              XToast.error(LoginActivity.this,this.getString(R.string.yz_login_pwd)).show();
//          }

    @OnClick({R2.id.qk_back, R2.id.tv_save})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.qk_back) {
            if (!TextUtils.isEmpty(token)) {
                //
                XToast.error(UpdatePwdActivity.this, R.string.plz_up_pwd).show();
            } else {
                ActivityTool.getInstance().popOneActivity(this);
            }
        } else if (id == R.id.tv_save) {
            if (TextUtils.isEmpty(etOld.getText().toString().trim()) || TextUtils.isEmpty(etNew.getText().toString().trim()) || TextUtils.isEmpty(etRenew.getText().toString().trim())) {
                XToast.error(this, getString(R.string.pwd_is_not_null)).show();
            } else if (!etNew.getText().toString().trim().equals(etRenew.getText().toString().trim())) {
                XToast.error(this, getString(R.string.pwd_is_not_same)).show();

            } else {
                PostPwd postPwd = new PostPwd(etOld.getText().toString().trim(), etNew.getText().toString().trim(), etRenew.getText().toString().trim());


                if (!TextUtils.isEmpty(token)) {
                    //
                    mUpdatePwdViewModel.Loginsave(postPwd, token, new BasicViewModel.MyBasicDataCallBack<Object>() {
                        @Override
                        public void onDataSuccessCallBack(Object o) {
                            XToast.success(UpdatePwdActivity.this, getString(R.string.update_pwd_success)).show();
                            ARouter.getInstance().build("/login/LoginActivity").navigation();
                            ActivityTool.getInstance().popOneActivity(UpdatePwdActivity.this);

                        }

                        @Override
                        public void onDataFailCallBack(Object o) {
                            String error = (String) o;
                            if ("300".equals(error)) {
                                XToast.error(UpdatePwdActivity.this, getString(R.string.old_pwd_error)).show();
                            } else if ("303".equals(error)) {
                                XToast.error(UpdatePwdActivity.this, getString(R.string.gs)).show();
                            } else {
                                XToast.error(UpdatePwdActivity.this, getString(R.string.update_pwd_fail)).show();
                            }

                        }
                    });
                } else {
                    mUpdatePwdViewModel.save(postPwd, new BasicViewModel.MyBasicDataCallBack<Object>() {
                        @Override
                        public void onDataSuccessCallBack(Object o) {
                            XToast.success(UpdatePwdActivity.this, getString(R.string.update_pwd_success)).show();
                            ActivityTool.getInstance().popOneActivity(UpdatePwdActivity.this);

                        }

                        @Override
                        public void onDataFailCallBack(Object o) {
                            String error = (String) o;
                            if ("300".equals(error)) {
                                XToast.error(UpdatePwdActivity.this, getString(R.string.old_pwd_error)).show();
                            } else if ("303".equals(error)) {
                                XToast.error(UpdatePwdActivity.this, getString(R.string.gs)).show();
                            } else {
                                XToast.error(UpdatePwdActivity.this, getString(R.string.update_pwd_fail)).show();
                            }

                        }
                    });
                }


            }
        }
    }

}
